<?php
class SubscriptionController extends BackendController
{

    public $active = "plugins-subscription";
    public $plug_version = "1.0";
    public $plug_title = "网站订阅";
    public $plug_description = "网站订阅";
    public $plug_controller = "subscription";


    public function actionIndex(){

        $this->data['subscriptions'] = $this->connection->createCommand("select * from rk_plug_subscription")
            ->queryAll();

        $this->render("index",$this->data);
    }


    public function actionInstall(){

        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_customer`")->execute();
        $this->connection->createCommand("CREATE TABLE `rk_plug_customer` (
  `plug_customer_id` int(11) NOT NULL AUTO_INCREMENT,
  `image_address` varchar(255) DEFAULT NULL,
  `content` text,
  `title` varchar(255) DEFAULT NULL,
  `position` varchar(255) DEFAULT NULL,
  `seq` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`plug_customer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8")->execute();
        $this->connection->createCommand("insert into rk_plug (plug_controller,plug_version,plug_key) values (:controller,:version,:key)")
            ->bindParam(":controller",$this->plug_controller)
            ->bindParam(":version",$this->plug_version)
            ->bindParam(":key",$this->active)
            ->execute();
        $parentMenuId = $this->connection->createCommand("SELECT menu_id FROM rk_menu WHERE menu_key = 'plugins'")->queryScalar();
        $this->connection->createCommand("INSERT INTO rk_menu ( menu_text, parent_id, url, menu_key ) VALUES (:menu_text,:parent_id,'index.php?r=plugins/subscription',:menu_key)")
            ->bindParam(":menu_text",$this->plug_title)
            ->bindParam(":parent_id",$parentMenuId)
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

    public function actionUninstall(){
        $this->connection->createCommand("delete from rk_plug where plug_key = :plug_key")
            ->bindParam(":plug_key",$this->active)
            ->execute();
        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_service`")->execute();
        $this->connection->createCommand("delete from rk_menu where menu_key = :menu_key")
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

}